<?php

namespace App\Admin\Controllers\Log;

use App\Admin\Extensions\VoiceCallOp;
use App\Http\Controllers\Controller;
use App\Model\Log\CallbackLogAogra;
use App\Model\VideoCall;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Layout\Content;

class AgoraLogController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('声网回调日志记录');
            $content->description('');
            $content->body($this->grid());
        });
    }

    /**
     *  $table->string('channel_id', 40)->default('')->comment('频道ID');
            $table->integer('uid')->default(0)->comment('消息主体用户UID');
            $table->unsignedTinyInteger('msg_type')->default(0)->comment('消息类型');
            $table->text('detail')->comment('回调信息');
     */
    protected function grid()
    {
        $grid = new Grid(new CallbackLogAogra());
        $grid->model()->with('eventUser')->orderBy('id', 'desc');
        $grid->id('记录ID');
        $grid->channel_id('频道ID');
        $grid->msg_type('事件类型')->display(function ($data) {
            return CallbackLogAogra::EVENT_TYPE[$data] . "（{$data}）";
        });
        $grid->detail('详情')->display(function ($data) {
            return json_decode($data, true);
        });
        $grid->eventUser()->user_profile_id('事件用户Profile-ID');
        $grid->uid('事件用户UID');
        $grid->eventUser()->nick_name('事件用户昵称');
        $grid->created_at('回调时间')->display(function ($time) {
            return !$time ? '-' : date('Y-m-d H:i:s', strtotime($time));
        });

        //禁用导出
        $grid->disableExport();
        //禁用行选择
        $grid->disableRowSelector();
        // $grid->disableActions();
        $grid->disableCreateButton();
        $grid->filter(function ($filter) {
            $filter->expand();
            $filter->disableIdFilter();
            $filter->column(1 / 2, function ($filter) {
                $filter->equal('eventUser.user_id', '事件user_id');
                $filter->equal('msg_type', '事件类型')->select(CallbackLogAogra::EVENT_TYPE);
                $filter->between('created_at', '发起时间')->datetime();
            });
            $filter->column(1 / 2, function ($filter) {
                $filter->equal('channel_id', '频道ID');
                $filter->equal('call_type', '话单类型')->select([0 => '语音', 1 => '视频']);
            });
        });
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableDelete();
            $actions->disableEdit();
            $actions->disableView();
        });
        return $grid;
    }
}
